﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using WLMCoreLibrary;
using SAW_Core;
using System.Reflection;

namespace SAWPOS
{
    // A delegate type for hooking up change notifications.
    public delegate void SellNoteSelectedHandler(object sender, DataGridViewCellEventArgs e);
    public delegate void InvoiceSelectedHandler(object sender, DataGridViewCellEventArgs e);

    public partial class ListadoNotas : Form
    {
        public event SellNoteSelectedHandler SellNoteSelected;

        public event InvoiceSelectedHandler InvoiceSelected;

        private bool showInvoices;
        private DataTable datosTabla;

        public ListadoNotas()
        {
            InitializeComponent();
        }

        private void ListadoNotas_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Loads the sell notes.
        /// </summary>
        public void loadSellNotes()
        {
            dbConnector conexion = new dbConnector(ConfigurationSettings.AppSettings["cadenaConexion"].ToString());
            VentaCollection lista= new VentaCollection(ref conexion);
            conexion.CloseConnection();
            gridProductos.AutoGenerateColumns = false;
            datosTabla = ListToDataTable(lista.Elementos);
            //          
            gridProductos.DataSource = datosTabla.DefaultView;

            showInvoices = false;
        }


        public void loadInvoices()
        {
            dbConnector conexion = new dbConnector(ConfigurationSettings.AppSettings["cadenaConexion"].ToString());
            FacturaCollection lista= new FacturaCollection(true, ref conexion);
            conexion.CloseConnection();
            gridProductos.Columns[1].DataPropertyName = "FullFolio";
            gridProductos.Columns[3].DataPropertyName = "R_RazonSocial";
            gridProductos.AutoGenerateColumns = false;

            datosTabla = ListToDataTable(lista.Elementos);
            gridProductos.DataSource = datosTabla.DefaultView;
            showInvoices = true;
        }

        /// <summary>
        /// Handles the CellContentClick event of the gridProductos control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellEventArgs"/> instance containing the event data.</param>
        private void gridProductos_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int idNotaVenta=Convert.ToInt32(gridProductos.Rows[e.RowIndex].Cells[0].Value);
                if (showInvoices)
                    InvoiceSelected(sender, e);
                else
                    SellNoteSelected(sender, e);
            }
            catch { }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            
        }

        private DataTable ListToDataTable<T>(List<T> list)
        {
            DataTable dt = new DataTable();

            foreach (PropertyInfo info in typeof(T).GetProperties())
            {
                dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
            }
            foreach (T t in list)
            {
                DataRow row = dt.NewRow();
                foreach (PropertyInfo info in typeof(T).GetProperties())
                {
                    row[info.Name] = info.GetValue(t, null);
                }
                dt.Rows.Add(row);
            }
            return dt;
        }

        private void ChangeSearchFilter()
        {
            try
            {
                string valorBusqueda=txtSearchBox.Text;
                DateTime fechaBusqueda=DateTime.Now;
                int idRegistro=0;
                if (DateTime.TryParse(valorBusqueda, out fechaBusqueda))
                {
                    datosTabla.DefaultView.RowFilter = " FechaCreacion >= #" + fechaBusqueda.ToString("yyyy-MM-dd 00:00:00") + "# AND FechaCreacion < #" + fechaBusqueda.ToString("yyyy-MM-dd 23:59:59") + "#";
                }
                else
                {
                    if (showInvoices)
                    {
                        datosTabla.DefaultView.RowFilter = "R_RazonSocial LIKE '%" + valorBusqueda + "%' OR FullFolio like'%" + valorBusqueda + "%'";
                    }
                    else
                    {
                        if (int.TryParse(valorBusqueda, out idRegistro))
                            datosTabla.DefaultView.RowFilter = " id=" + idRegistro + "";
                        else
                            datosTabla.DefaultView.RowFilter = "NombreCliente LIKE '%" + valorBusqueda + "%'";
                    }
                }
                if (valorBusqueda.Length == 0)
                    datosTabla.DefaultView.RowFilter = "";
                //  
            }
            catch (Exception ex)
            {
            
            }
        }

        private void txtSearchBox_TextChanged(object sender, EventArgs e)
        {
            ChangeSearchFilter();
        }
    }
}
